﻿/// <reference path="core/Error.js"/>
/// <reference path="FaScript.js"/>
/// <reference path="FaScript.Event.js"/>

"use strict";
Namespace('FaScript');

FaScript.defineClass('Page', ['window'],
	function () {
	    var object = Object.create(null);

	    //私有变量

	    //属性
	    Object.defineProperties(object,
            {
                'OnPopStateChange': { value: new FaScript.Event('popstate') }
            });

	    //方法
	    Object.defineProperty(object, 'pjax', {
	        value: function () {
	            /// <summary>启用页面预加载模式</summary>

	            window.onpopstate = function (event) {
	                object.OnPopStateChange.trigger(window, event.state);
	            };
	            window.onprogress = function () {
	                
	            };

	            // 处理所有A标签
	            $('a').each(function (index, obj) {
	                var $link = $(obj);
	                var href = $link.attr('href');
	                
	                if (!href.startWith('#')) {
	                    $link.click(function () {
	                        var state = {};
	                        window.history.pushState(state, '', this.href);
	                        object.OnPopStateChange.trigger(window, state);
	                        return false;
	                    });
	                }
	            });	            
	        }
	    });

        //锁定成员
	    Object.seal(object);
	    return object;
	});